﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Shuffle.cs" company="Luis de la Torre">
//   Copyright Luis de la Torre, unless otherwise indicated
// </copyright>
// <summary>
//   Defines the Shuffler type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace PVRP_SR
{
    using System;

    /// <summary>
    /// The shuffler.
    /// </summary>
    public class Shuffler
    {
        #region Static Fields

        /// <summary>
        /// Used in Shuffle(T).
        /// </summary>
        private static readonly Random RandomGenerator = new Random();

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// Shuffle the array.
        /// </summary>
        /// <typeparam name="T">
        /// Array element type.
        /// </typeparam>
        /// <param name="array">
        /// Array to shuffle.
        /// </param>
        public static void Shuffle<T>(T[] array)
        {
            var random = RandomGenerator;
            for (int i = array.Length; i > 1; i--)
            {
                // Pick RandomGenerator element to swap.
                int j = random.Next(i); // 0 <= j <= i-1

                // Swap.
                T tmp = array[j];
                array[j] = array[i - 1];
                array[i - 1] = tmp;
            }
        }

        #endregion
    }
}